package com.zzyl.serve.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.core.redis.RedisCache;
import com.zzyl.serve.domain.NursingLevel;
import com.zzyl.serve.mapper.NursingLevelMapper;
import com.zzyl.serve.service.INursingLevelService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 护理等级Service业务层处理
 *
 */
@Service
@Slf4j
public class NursingLevelServiceImpl extends ServiceImpl<NursingLevelMapper, NursingLevel> implements INursingLevelService {

    @Autowired
    private NursingLevelMapper nursingLevelMapper;

    /**
     * 查询护理等级
     *
     * @param id 护理等级主键
     * @return 护理等级
     */
    @Override
    public NursingLevel selectNursingLevelById(Integer id) {
        return nursingLevelMapper.selectById(id);
    }

    /**
     * 查询护理等级列表
     *
     * @param nursingLevel 护理等级
     * @return 护理等级
     */
    @Override
    public List<NursingLevel> selectNursingLevelList(NursingLevel nursingLevel) {
        return nursingLevelMapper.selectNursingLevelList(nursingLevel);
    }

    /**
     * 新增护理等级
     *
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    @CacheEvict(cacheNames = "nursingLevel", allEntries = true)
    public int insertNursingLevel(NursingLevel nursingLevel) {
        return nursingLevelMapper.insert(nursingLevel);
    }

    /**
     * 修改护理等级
     *
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    @CacheEvict(cacheNames = "nursingLevel", allEntries = true)
    public int updateNursingLevel(NursingLevel nursingLevel) {
        return nursingLevelMapper.updateById(nursingLevel);
    }

    /**
     * 批量删除护理等级
     *
     * @param ids 需要删除的护理等级主键
     * @return 结果
     */
    @Override
    @CacheEvict(cacheNames = "nursingLevel", allEntries = true)
    public int deleteNursingLevelByIds(Integer[] ids) {
        return nursingLevelMapper.deleteBatchIds(List.of(ids));
    }

    /**
     * 删除护理等级信息
     *
     * @param id 护理等级主键
     * @return 结果
     */
    @Override
    @CacheEvict(cacheNames = "nursingLevel", allEntries = true)
    public int deleteNursingLevelById(Integer id) {
        return nursingLevelMapper.deleteById(id);
    }

    @Autowired
    private RedisTemplate redisTemplate;

    private static final String CACHE_KEY_PREFIX = "nursingLevel:all";

    /**
     * 查询所有的护理等级, 状态为启用
     *
     * @return
     */
    @Override
    @Cacheable(cacheNames = "nursingLevel", key = "'all'")
    public List<NursingLevel> listAll() {
       /* //1.从redis缓存中查询数据
        List<NursingLevel> list = (List<NursingLevel>)redisTemplate.opsForValue().get(CACHE_KEY_PREFIX);
        if(null != list){
            System.out.println("=================== 缓存命中 =====================");
            // 缓存命中
            return list;
        }*/
        //缓存没命中
        // 构建查询条件
        List<NursingLevel> list = lambdaQuery().eq(NursingLevel::getStatus, 1).list();
        System.out.println("=============== 缓存没命中 从数据库查 ===================");
        // 查完数据库要存入redis
        //redisTemplate.opsForValue().set(CACHE_KEY_PREFIX, list);
        return list;
    }


}
